package com.shuql.zklock.config;

import com.shuql.zklock.ZkLock;
import com.shuql.zklock.impl.ZKDistrbuteLock;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(ZkLock.class)
@EnableConfigurationProperties(ZkLockProperties.class)
@ConditionalOnProperty(prefix = "zklock", value = "enabled", matchIfMissing = true)
public class ZkLockAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ZkLock lock(ZkLockProperties zkLockProperties) {
        return new ZKDistrbuteLock(zkLockProperties.getHost(), zkLockProperties.getPort());
    }

}
